﻿using System;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;

namespace DnPakExplorer {
	public partial class DnPakFileSelectForm : Form {
		public String[] SelectedFiles {
			get {
				if (_selectedFiles == null) {
					return new string[0];
				}
				return _selectedFiles;
			}
		}
		String[] _selectedFiles;

		public DnPakFileSelectForm() {
			InitializeComponent();
		}

		private void DnPakFileSelectForm_Load(object sender, EventArgs e) {
			RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\snda\DragonNest", false);
			String dnPath = null;
			if (key != null) {
				dnPath = key.GetValue("Path") as string;
				key.Close();
			}
			if (dnPath == null) {
				FolderBrowserDialog dlg = new FolderBrowserDialog();
				dlg.Description = "龙之谷安装路径";
				dlg.ShowNewFolderButton = false;
				if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK) {
					this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
					this.Close();
					return;
				} else {
					dnPath = dlg.SelectedPath;
					this.TopMost = true;
				}
			}
			string[] paths = Directory.GetFiles(dnPath, "Resource*.pak");
			mListView.Items.Clear();
			foreach (string path in paths) {
				ListViewItem item = mListView.Items.Add(path);
				item.SubItems.Add(Util.FileSizeToString(new FileInfo(path).Length));
				item.Checked = true;
			}
		}

		private void button1_Click(object sender, EventArgs e) {
			this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.Close();
		}

		private void button2_Click(object sender, EventArgs e) {
			_selectedFiles = new string[mListView.CheckedItems.Count];
			int i=0;
			foreach (ListViewItem item in mListView.CheckedItems) {
				_selectedFiles[i++] = item.Text;
			}
			this.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.Close();
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e) {
			foreach (ListViewItem item in mListView.Items) {
				item.Checked = checkBox1.Checked;
			}	
		}

		private void mListView_MouseDoubleClick(object sender, MouseEventArgs e) {
			ListViewItem item = null;
			if((item=mListView.GetItemAt(e.X, e.Y)) != null){
				_selectedFiles = new string[] { item.Text };
				this.DialogResult = System.Windows.Forms.DialogResult.OK;
				this.Close();
			}
		}
	}
}
